home *** CD-ROM | disk | FTP | other *** search
- /* Program PROGCHK.C */
-
- /* This program scans the "C" source file for possible */
- /* syntax errors. Even though the results may not always*/
- /* be accurate, it could isolate a hard-to-find bug. */
- /* This routine checks for: */
- /* 1. Unmatched braces. */
- /* 2. Unmatched parentheses. */
- /* 3. Unmatched single and double quotes. */
- /* 4. Nested comments. */
-
- /* This routine also provides comment and total line counts. */
-
-
- #define NULL 0
- #define TRUE 1
- #define FALSE 0
- #define EOF (-1)
- #define SEMI 59
- #define TAB 9
-
- typedef int FILE;
- char stra[100];
- int lincnt, erflag, linptr;
- char linbuf[256];
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- int c=0, i=0, lefpar=0, rigpar=0;
- int dquote=0,lefbra=0,rigbra=0;
- int comcnt=0,incom=0,comyes=0,squote=0;
- int prevchr=0, nestyes=0;
- FILE *in_file;
-
- if (argc < 2)
- {
- printf (" This program checks for possible syntax errors.\n");
- printf (" To invoke it's use: E>progchk filename.c \n\n");
- exit(1);
- }
- if ((in_file = fopen(argv[1],"r")) == NULL)
- {
- printf (" Cannot open %s for input.\n",argv[1]);
- exit(1);
- }
-
- lincnt = 0; /* Setup variables */
- linptr = 0;
- erflag = FALSE;
- for (i=0; i<255; ++i )
- linbuf[i] = NULL;
-
- while (( c = fgetc(in_file)) != EOF ) /* Main loop */
- {
-
- switch (c)
- {
- case '{':
- lefbra++;
- break;
- case '}':
- rigbra++;
- break;
- case '(':
- lefpar++;
- break;
- case ')':
- rigpar++;
- break;
- case '/':
- if (prevchr == '*')
- {
- if (incom == FALSE)
- nestyes = TRUE;
- incom = FALSE;
- comyes = TRUE;
- }
- break;
- case '*':
- if (prevchr == '/')
- {
- if (incom == TRUE)
- nestyes = TRUE;
- incom = TRUE;
- comyes = TRUE;
- }
- break;
- case '\'':
- if (squote == TRUE )
- squote = FALSE;
- else
- squote = TRUE;
- break;
- case '\"':
- if (dquote == TRUE )
- dquote = FALSE;
- else
- dquote = TRUE;
- break;
- case '\n' :
- lincnt++;
- if (squote == TRUE)
- {
- lerr("possible unbalanced single quotes.");
- squote = FALSE;
- }
- if (dquote == TRUE)
- {
- lerr("possible unbalanced double quotes.");
- dquote = FALSE;
- }
- if (incom == TRUE || nestyes == TRUE)
- {
- lerr("possible nested comments.");
- incom = FALSE;
- nestyes = FALSE;
- }
- if (comyes == TRUE)
- {
- comcnt++;
- comyes = FALSE;
- }
- if (lefpar != rigpar)
- {
- lerr("possible unbalanced parens.");
- lefpar = 0;
- rigpar = 0;
- }
- for (i=0; i < 255; ++i )
- linbuf[i] = NULL;
- erflag = FALSE;
- linptr = 0;
- break;
- }
- prevchr = c;
- if ((i = isprint(c)) == TRUE || c == TAB)
- linbuf[linptr] = c;
- linptr++;
- }
-
- printf ("\n\n");
- if(lefbra > rigbra)
- printf (" --- File has unbalanced braces---too many {. \n");
- if (rigbra > lefbra)
- printf (" --- File has unbalances braces---too many }. \n");
-
- printf ("\n\n File: %s contains %d comment lines and %d total lines.\n",argv[1],comcnt,lincnt);
- fclose (in_file);
- }
-
- /* Error function. */
-
- lerr(stra)
- char stra;
- {
- int i;
- if (erflag == FALSE)
- {
- printf ("Line: %d >>>> ",lincnt);
- for (i=0; linbuf[i] == NULL || i < 255; ++i)
- printf ("%c",linbuf[i]);
- printf ("\n");
- erflag = TRUE;
- }
- printf (" --- %s \n",stra);
- }
- L || i < 255; ++i)
- printf ("%c",linbuf[i])